package com.blog.backend.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blog.backend.config.MqttGateway;
import com.blog.backend.model.EquipInfo;
import com.blog.backend.model.FaceInfo;
import com.blog.backend.service.ArticlesService;
import com.blog.backend.service.impl.ArticlesServiceImpl;
import org.apache.commons.codec.binary.Base64;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Date;

@RestController
public class TestWwController {
 
    @Resource
    private MqttGateway mqttGateway;

    @Resource
    private ArticlesService articlesService;
 
    @GetMapping("/test/sendMqtt")
    public String sendMqtt(){
        String topic="/maxvision/sys/dce37662f1b54699976869bae5572610000011/92d2e50e9732462dab98965a7c9bd240000011/thing/faceInfo/add";
        FaceInfo info = new FaceInfo();
        String time = String.valueOf(new Date().getTime());
        String encryptKey="123456";
        String encryptText="version=1.0&clientId=f58a8a8047f849a00011&productKey=dce37662f1b54699976869bae5572610000011&deviceName=92d2e50e9732462dab98965a7c9bd240000011&timeStamp="+time;
        info.setFaceid("112");
        info.setDeviceName("92d2e50e9732462dab98965a7c9bd240000011");
        info.setFaceName("方龚赛");
        info.setTimeStamp(time);
        info.setProductKey("dce37662f1b54699976869bae5572610000011");
        info.setFeaturesVersion("1.0");
        byte[] data= new byte[0];
        byte[] bytes=null;
        try {
            data = encryptKey.getBytes("UTF-8");
            //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
            SecretKey secretKey = new SecretKeySpec(data, "HmacSHA1");
            //生成一个指定 Mac 算法 的 Mac 对象
            Mac mac = Mac.getInstance("HmacSHA1");
            //用给定密钥初始化 Mac 对象
            mac.init(secretKey);
            byte[] text = encryptText.getBytes("UTF-8");
            bytes = mac.doFinal(text);
            String str = new String(Base64.encodeBase64(bytes));
            info.setSign(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

        EquipInfo equipInfo = new EquipInfo();
        equipInfo.setVersion("1.0");
        equipInfo.setMethod("thing.faceInfo.add");
        equipInfo.setParams(info);
        String string = JSONObject.toJSONString(equipInfo);
        //System.out.println("消息订阅"+sendData);
        mqttGateway.sendToMqtt(string,topic);
        return "OK";
    }
 
    @RequestMapping("/test")
    public String test(String  sendData){
        return "testOK";
    }

    @GetMapping("/sss")
    public void testNew(){
        this.articlesService.notify();
        ArticlesServiceImpl service = new ArticlesServiceImpl();
        service.deleteArticle("1234");
    }
 
}
